package k8s

import (
	"github.com/gin-gonic/gin"
	configmapreq "kubeimooc/model/configmap/request"
	"kubeimooc/response"
)

type ConfigMapApi struct {
}

/*
* 创建或者修改
 */
func (r *ConfigMapApi) CreateOrUpdateConfigMap(c *gin.Context) {
	var configMapReq configmapreq.ConfigMap
	err := c.ShouldBindJSON(&configMapReq)
	if err != nil {
		response.FailWithMessage(c, "ConfigMap解析失败")
		return
	}
	err = configMapService.CreateOrUpdateConfigMap(configMapReq)
	if err != nil {
		response.FailWithMessage(c, err.Error())
	} else {
		response.Success(c)
	}
}

func (r *ConfigMapApi) GetConfigMapDetailOrList(c *gin.Context) {
	namespace := c.Param("namespace") //命名空间
	name := c.Query("name")           //configmap名称
	keyword := c.Query("keyword")     //搜索关键字
	if name == "" {
		//获取列表
		list, err := configMapService.GetConfigMapList(namespace, keyword)
		if err != nil {
			response.FailWithMessage(c, err.Error())
		} else {
			response.SuccessWithDetail(c, "返回configmap list 成功", list)
		}
	} else {
		//获取configmap详情
		detail, err := configMapService.GetConfigMapDetail(namespace, name)
		if err != nil {
			response.FailWithMessage(c, err.Error())
		} else {
			response.SuccessWithDetail(c, "返回configmap详情成功", detail)
		}
	}
}

/*
*
删除configMap
*/
func (r *ConfigMapApi) DeleteConfigMap(c *gin.Context) {
	namespace := c.Param("namespace")
	name := c.Param("name")
	err := configMapService.DeleteConfigMap(namespace, name)
	if err != nil {
		response.FailWithMessage(c, err.Error())
	} else {
		response.Success(c)
	}
}
